package org.liqingye.es.exception;

import org.elasticsearch.ElasticsearchException;
import org.liqingye.es.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 
 * @author ruoyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 请求方式不支持
     */
    @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    public JsonResult<String> handleException(HttpRequestMethodNotSupportedException e) {
        log.error(e.getMessage(), e);
        return new JsonResult<String>(false, "请求异常", e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public JsonResult<String> notFount(RuntimeException e) {
        log.error("运行时异常:", e);
        return new JsonResult<String>(false, "请求异常", e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public JsonResult<String> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return new JsonResult<String>(false, "请求异常", e.getMessage());
    }

    /**
     * Elasticsearch api处理异常
     */
    @ExceptionHandler(ElasticsearchException.class)
    public JsonResult<String> businessException(ElasticsearchException e) {
        log.error(e.getMessage(), e);
        return new JsonResult<String>(false, "请求异常", e.getMessage());
    }

}
